<template>
  <div style="width: 100%;" id="home">
    <div style="width: 400px;margin: 100px auto">
      <div style="color: #cccccc;font-size: 30px;text-align: center;padding: 30px 0">欢迎登录线上点餐系统</div>
      <el-form ref="form" :model="form" :rules="rules" status-icon size="large">
        <el-form-item prop="loginAct">
          <el-input v-model="form.loginAct" placeholder="请输入账号">
            <template #prefix>
              <el-icon class="el-input__icon"><Avatar /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="loginPwd">
          <el-input v-model="form.loginPwd" show-password placeholder="请输入密码">
            <template #prefix>
              <el-icon class="el-input__icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <div style="display: flex">
            <el-input prefix-icon="el-icon-key" v-model="form.validCode" style="width: 50%;" placeholder="请输入验证码"></el-input>
            <ValidCode @input="createValidCode" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="login">登 录</el-button>
          <el-form-item><el-button type="text" @click="$router.push('front/register')">前往注册 >> </el-button></el-form-item>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {Avatar,Lock} from "@element-plus/icons";
import request from "@/utils/request";
import ValidCode from "@/components/ValidCode";

export default {
  name: "Login",
  components: {
    Avatar,Lock,ValidCode,
  },
  created() {
    sessionStorage.removeItem("user")
  },
  data() {
    return {
      form :{},
      rules: {
        loginAct: [
          { required: true, message: '请输入账号!', trigger: 'blur' }
        ],
        loginPwd: [
          { required: true, message: '请输入密码!', trigger: 'blur' }
        ],
      },
      validCode: ''
      // 加背景图片
      // bg: {
      //   backgroundImage: "url(" + require("@/assets/bg.jpg") + ")",
      //   backgroundRepeat: "no-repeat",
      //   backgroundSize: "100% 100%"
      // }
    }
  },
  methods: {
    // 接收验证码组件提交的 4位验证码
    createValidCode(data) {
      this.validCode = data
    },
    login(){
      this.$refs['form'].validate((valid) => {
        //表单验证必要
        if (!this.form.validCode) {
          this.$message.error("请填写验证码")
          return
        }
        if(this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
          this.$message.error("验证码错误")
          return
        }
        if(valid){
          request.post("order/user/login", this.form).then(res =>{
            sessionStorage.setItem("user",JSON.stringify(res.data.user))
            sessionStorage.setItem("token",JSON.stringify(res.data.token))
            this.msgTip("登录",res);
          });
        }
      })
    },
    msgTip(msg,res){
      if(res.code == 200){
        this.$message({
          type:"success",
          message: msg+"成功"
        })
        this.$router.push("/front/home");
      }else{
        this.$message({
          type:"error",
          message:res.data.msg
        })
      }
    }
  }
}

</script>

<style scoped>
  #home {
    background: url("../../assets/q.jpeg");
    width: 100%;
    height: 100vh;
    position: fixed;
    background-size: 100%,100%;
  }
</style>